package com.sanhuo.triild.provider;


import com.sanhuo.utils.basic.StringUtil;

public class SQL {
    private static final String AND = " AND ";
    private static final String OR = " OR ";
    private StringBuilder sql = new StringBuilder();

    public static SQL getSQL() {
        return new SQL();
    }

    public SQL select(String... columns) {
        sql.append("select ");
        for (String column : columns) {
            sql.append(column).append(",");
        }
        sql = new StringBuilder(sql.substring(0, sql.lastIndexOf(",")));
        return this;
    }

    public SQL select() {
        sql.append("select * ");
        return this;
    }

    public SQL orderBy(String condition, String sorf) {
        if (StringUtil.isBlank(sorf)) {
            return orderBy(condition);
        }
        sql.append(" order by ").append(condition).append(" ").append(sorf);
        return this;
    }

    public SQL orderBy(String condition) {
        sql.append(" order by ").append(condition).append(" desc");
        return this;
    }

    public SQL before(String sql) {
        this.sql.append(sql);
        return this;
    }

    public SQL count(String column) {
        sql.append("(").append(column).append(") ");
        return this;
    }

    public SQL delete() {
        sql.append("delete ");
        return this;
    }

    public SQL from(String tableName) {
        sql.append(" from ").append(tableName);
        sql.append(" where 1=1 ");
        return this;
    }

    public SQL where(String condition, String conn) {
        if (StringUtil.isBlank(conn)) {
            return where(condition);
        }
        sql.append(conn).append(condition);
        return this;
    }

    public SQL where(String condition) {
        sql.append(AND).append(condition);
        return this;
    }

    public SQL add(String str) {
        sql.append(str);
        return this;
    }

    public SQL ORDER_BY(String condition) {
        sql.append(" order by ").append(condition);
        return this;
    }

    public String build() {
        return sql.toString();
    }

    public SQL insert(String tableName) {
        sql.append("insert into ").append(tableName);
        return this;
    }

    public SQL Insert_column(String... columns) {

//        if (columns.length > 0) {
        sql.append(" ( ");
        for (String column : columns) {
            sql.append(column).append(",");
        }
        sql = new StringBuilder(sql.substring(0, sql.lastIndexOf(",")));
        sql.append(" ) ");
//        }
        return this;
    }

    public SQL value(String... values) {
        sql.append("values (");
        for (String value : values) {
            sql.append(value).append(",");
        }
        sql = new StringBuilder(sql.substring(0, sql.lastIndexOf(",")));
        sql.append(" ) ");
        return this;
    }


    public SQL update(String tableName) {
        sql.append("update ").append(tableName).append(" set ");
        return this;
    }

    public SQL set(String... conditions) {
        for (String condition : conditions) {
            sql.append(condition).append(",");
        }
        sql = new StringBuilder(sql.substring(0, sql.lastIndexOf(",")));
        sql.append(" where 1 = 1");
        return this;
    }

    protected String buildCondition(String columnName) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(columnName).append(" = #{").append(columnName).append("}");
        return stringBuilder.toString();
    }

    String buildps(String columnName) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("#{").append(columnName).append("}");
        return stringBuilder.toString();
    }


}
